package com.luobr.backend.exception;

import com.luobr.backend.common.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * 抛出异常工具类
 * 简化异常判断语句，类似断言的效果
 */
@Slf4j
public class ThrowUtils {

    /**
     * 条件成立则抛异常
     *
     * @param condition        条件
     * @param runtimeException 异常
     */
    public static void throwIf(boolean condition, RuntimeException runtimeException) {
        if (condition) {
            throw runtimeException;
        }
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition        条件
     * @param runtimeException 异常
     * @param logMessage       日志信息
     */
    public static void throwIf(boolean condition, RuntimeException runtimeException, String logMessage) {
        // 日志打印
        log.info(logMessage);
        throwIf(condition, runtimeException);
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition        条件
     * @param runtimeException 异常
     * @param logger           可以确定日志记录所在类
     * @param logMessage       日志信息
     */
    public static void throwIf(boolean condition, RuntimeException runtimeException, Logger logger, String logMessage) {
        // 日志打印
        logger.info(logMessage);
        throwIf(condition, runtimeException);
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition 条件
     * @param errorCode 错误码
     */
    public static void throwIf(boolean condition, ResponseCode errorCode) {
        throwIf(condition, new BusinessException(errorCode));
    }

    /**
     * 条件成立则抛异常
     *
     * @param condition 条件
     * @param errorCode 错误码
     * @param message   具体报错信息
     */
    public static void throwIf(boolean condition, ResponseCode errorCode, String message) {
        throwIf(condition, new BusinessException(errorCode, message));
    }
}
